package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.LbsTerminalRpcService;
import com.hunttown.mes.rpc.domain.LbsTerminalDTO;
import com.hunttown.mes.rpc.domain.query.LbsTerminalDTOQuery;
import com.hunttown.mes.service.LbsTerminalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.LbsTerminalConverts.convert;

/**
 * created by wangjunfu on 2021-07-05 11:06:22
 */
@Service
public class LbsTerminalRpcServiceImpl implements LbsTerminalRpcService {

    private final Logger logger = LoggerFactory.getLogger(LbsTerminalRpcServiceImpl.class);

    private final LbsTerminalService lbsTerminalService;

    @Autowired
    public LbsTerminalRpcServiceImpl(LbsTerminalService lbsTerminalService) {
        this.lbsTerminalService = lbsTerminalService;
    }

    @Override
    public LbsTerminalDTO insert(LbsTerminalDTO lbsTerminalDTO) {
        try {
            return convert(lbsTerminalService.insert(convert(lbsTerminalDTO)));
        } catch (Exception e) {
            logger.error("LbsTerminal create failed.", e);
            throw new RuntimeException("LbsTerminal create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(LbsTerminalDTO lbsTerminalDTO) {
        try {
            return lbsTerminalService.updateInfoById(convert(lbsTerminalDTO));
        } catch (Exception e) {
            logger.error("LbsTerminal update by id failed.", e);
            throw new RuntimeException("LbsTerminal update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(LbsTerminalDTOQuery lbsTerminalDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsTerminalDTOQuery.getW_id() != null && lbsTerminalDTOQuery.getW_id() > 0) {
                query.put("w_id", lbsTerminalDTOQuery.getW_id());
            }

            if (lbsTerminalDTOQuery.getIds() != null && !lbsTerminalDTOQuery.getIds().equals("")) {
                query.put("ids", lbsTerminalDTOQuery.getIds());
            }

            if (lbsTerminalDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", lbsTerminalDTOQuery.getStopFlag());
            }

            if (lbsTerminalDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsTerminalDTOQuery.getDeleteFlag());
            }

            return lbsTerminalService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("LbsTerminal update by query failed.", e);
            throw new RuntimeException("LbsTerminal update by query failed, Please order your code.", e);
        }
    }

    @Override
    public LbsTerminalDTO selectById(Integer id) {
        try {
            return convert(lbsTerminalService.selectById(id));
        } catch (Exception e) {
            logger.error("LbsTerminal select by id failed.", e);
            throw new RuntimeException("LbsTerminal select by id failed, Please order your code.", e);
        }
    }

    @Override
    public LbsTerminalDTO selectObjByQuery(LbsTerminalDTOQuery lbsTerminalDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsTerminalDTOQuery.getEntId() != null) {
                query.put("entId", lbsTerminalDTOQuery.getEntId());
            }

            if (lbsTerminalDTOQuery.getCarId() != null) {
                query.put("carId", lbsTerminalDTOQuery.getCarId());
            }

            if (lbsTerminalDTOQuery.getUserId() != null) {
                query.put("userId", lbsTerminalDTOQuery.getUserId());
            }

            if (lbsTerminalDTOQuery.getTerminalCode() != null) {
                query.put("terminalCode", lbsTerminalDTOQuery.getTerminalCode());
            }

            if (lbsTerminalDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", lbsTerminalDTOQuery.getStopFlag());
            }

            if (lbsTerminalDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsTerminalDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(lbsTerminalDTOQuery.getOrderBy())) {
                query.put("orderBy", lbsTerminalDTOQuery.getOrderBy());
            }

            return convert(lbsTerminalService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("LbsTerminal select obj by query failed.", e);
            throw new RuntimeException("LbsTerminal select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public LbsTerminalDTO selectObjWithoutEnt(LbsTerminalDTOQuery lbsTerminalDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsTerminalDTOQuery.getEntId() != null) {
                query.put("entId", lbsTerminalDTOQuery.getEntId());
            }

            if (lbsTerminalDTOQuery.getCarId() != null) {
                query.put("carId", lbsTerminalDTOQuery.getCarId());
            }

            if (lbsTerminalDTOQuery.getUserId() != null) {
                query.put("userId", lbsTerminalDTOQuery.getUserId());
            }

            if (lbsTerminalDTOQuery.getTerminalCode() != null) {
                query.put("terminalCode", lbsTerminalDTOQuery.getTerminalCode());
            }

            if (lbsTerminalDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", lbsTerminalDTOQuery.getStopFlag());
            }

            if (lbsTerminalDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsTerminalDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(lbsTerminalDTOQuery.getOrderBy())) {
                query.put("orderBy", lbsTerminalDTOQuery.getOrderBy());
            }

            return convert(lbsTerminalService.selectObjWithoutEnt(query));

        } catch (Exception e) {
            logger.error("LbsTerminal select obj without ent failed.", e);
            throw new RuntimeException("LbsTerminal select obj without ent failed, Please order your code.", e);
        }
    }

    @Override
    public Page<LbsTerminalDTO> getListForPage(LbsTerminalDTOQuery lbsTerminalDTOQuery) {
        Query query = Query.page(lbsTerminalDTOQuery.getPn(), lbsTerminalDTOQuery.getPageSize());

        if (lbsTerminalDTOQuery.getId() != null && lbsTerminalDTOQuery.getId() > 0) {
            query.put("id", lbsTerminalDTOQuery.getId());
        }

        if (lbsTerminalDTOQuery.getIds() != null && !lbsTerminalDTOQuery.getIds().equals("")) {
            query.put("ids", lbsTerminalDTOQuery.getIds());
        }

        if (lbsTerminalDTOQuery.getEntId() != null) {
            query.put("entId", lbsTerminalDTOQuery.getEntId());
        }

        if (lbsTerminalDTOQuery.getServiceId() != null) {
            query.put("serviceId", lbsTerminalDTOQuery.getServiceId());
        }

        if (StringUtils.isNotBlank(lbsTerminalDTOQuery.getLikeName())) {
            query.put("likeName", lbsTerminalDTOQuery.getLikeName());
        }

        if (lbsTerminalDTOQuery.getTerminalType() != null) {
            query.put("terminalType", lbsTerminalDTOQuery.getTerminalType());
        }

        if (StringUtils.isNotBlank(lbsTerminalDTOQuery.getShowDate())) {
            query.put("showDate", lbsTerminalDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(lbsTerminalDTOQuery.getStartDate())) {
            query.put("startDate", lbsTerminalDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(lbsTerminalDTOQuery.getEndDate())) {
            query.put("endDate", lbsTerminalDTOQuery.getEndDate());
        }

        if (lbsTerminalDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", lbsTerminalDTOQuery.getStopFlag());
        }

        if (lbsTerminalDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", lbsTerminalDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(lbsTerminalDTOQuery.getOrderBy())) {
            query.put("orderBy", lbsTerminalDTOQuery.getOrderBy());
        }

        List<LbsTerminalDTO> list = convert(lbsTerminalService.selectByQuery(query));
        Page<LbsTerminalDTO> page = Page.from(lbsTerminalDTOQuery.getPn(), lbsTerminalDTOQuery.getPageSize(), list);
        page.setTotalItems(lbsTerminalService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return lbsTerminalService.deleteById(id);
        } catch (Exception e) {
            logger.error("LbsTerminal delete failed.", e);
            throw new RuntimeException("LbsTerminal delete failed, Please order your code.", e);
        }
    }

    @Override
    public LbsTerminalDTO createTerminal(Integer clientType, Integer userType, Integer userId, String mobile, Integer entId) {
        try {
            return convert(lbsTerminalService.createTerminal(clientType, userType, userId, mobile, entId));
        } catch (Exception e) {
            logger.error("LbsTerminal create teminal failed.", e);
            throw new RuntimeException("LbsTerminal create teminal failed, Please order your code.", e);
        }
    }
}